package elitech.vietnam.myfashion.dialog;

import android.app.Dialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.entites.Order;
import elitech.vietnam.myfashion.entites.OrderDetail;
import elitech.vietnam.myfashion.view.OrderDetailView;
import elitech.vietnam.myfashion.view.OrderDetailView.OrderDetailViewEvent;
import elitech.vietnam.myfashion.wsclient.Utilities;

public class OrderConfirmDialog extends Dialog implements OnClickListener {

	private MainActivity mContext;
	private String[] mArrPayment = { "After Delivery" };
	private Spinner mSpinPayment;
	private ArrayAdapter<String> mAdapterPayment;
	private TextView mTxtTotal;
	private Button mBtnDone;
	private LinearLayout mLayDetail;
	private Order mOrder;
	private double mTotal;
	private OrderConfirmDialog mDialog;

	public OrderConfirmDialog(MainActivity context) {
		super(context, R.style.Dialog);
		this.mContext = context;
		this.mOrder = mContext.mOrder;
		this.mDialog = this;

		setContentView(R.layout.confirm);
		mSpinPayment = (Spinner) findViewById(R.id.confirm_spinnerPayment);

		mTxtTotal = (TextView) findViewById(R.id.confirm_txtTotal);
		mLayDetail = (LinearLayout) findViewById(R.id.confirm_layDetail);
		mBtnDone = (Button) findViewById(R.id.confirm_txtBtnDone);

		mAdapterPayment = new ArrayAdapter<String>(context,
				android.R.layout.simple_spinner_item, mArrPayment);
		mAdapterPayment
				.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
		mSpinPayment.setAdapter(mAdapterPayment);

		mBtnDone.setOnClickListener(this);

		for (int i = 0; i < mOrder.ListDetail.size(); i++) {
			final OrderDetail detail = mOrder.ListDetail.get(i);
			mTotal = mTotal + (detail.getAmount());
			final OrderDetailView view = new OrderDetailView(mContext, detail);
			view.mBtnDelete.setVisibility(View.VISIBLE);
			view.setOnCancelEvent(new OrderDetailViewEvent() {

				@Override
				public void onCancel() {
					if (mOrder.ListDetail.size() > 1) {
						new WarningDialog(mContext, mContext
								.getString(R.string.confirmdeletebill)) {
							public void onClick(View v) {
								mLayDetail.removeView(view);
								mOrder.ListDetail.remove(detail);
								mTotal = mTotal - detail.getAmount();
								mTxtTotal.setText(Utilities
										.numberFormat(mTotal) + " VND");
								super.onClick(v);
								mContext.mLastCartCount = 0;
							};
						}.show();
					} else {
						new WarningDialog(mContext, mContext
								.getString(R.string.confirmdeletebill)) {
							public void onClick(View v) {
								mDialog.dismiss();
								mContext.mOrder = new Order();
								super.onClick(v);
								mContext.mLastCartCount = 0;
							};
						}.show();
					}
				}
			});
			mLayDetail.addView(view);
		}
		mTotal = mTotal + mOrder.ShippingFee;
		mTxtTotal.setText(Utilities.numberFormat(mTotal) + " VND");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.confirm_txtBtnDone:
			new ShippingDialog(mContext, false).show();
			mDialog.dismiss();
			break;
		}
	}

}
